/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.columndefine.service
 * FILE    NAME: ColumnDefineDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.api.columndefine.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.hero.core.constant.SysConstant;
import com.hero.core.entity.PageResult;
import com.hero.core.entity.ResponseEntity;
import com.hero.core.exception.AbstractGenericException;
import com.hero.common.web.excel.util.ExcelExport;
import com.hero.core.util.StringUtils;
import com.hero.core.util.ValidationUtil;
import com.jodinfo.outmirror.api.columndefine.entity.ColumnDefineEntity;

@Service(value="columnDefineBizService")
public class ColumnDefineBizServiceImpl implements ColumnDefineBizService {
    private static final Logger logger = LoggerFactory.getLogger(ColumnDefineBizServiceImpl.class);
 	
 	@Resource(name="columnDefineDmlService")
	private ColumnDefineDmlService columnDefineDmlService;
	@Resource(name="columnDefineQueryService")
	private ColumnDefineQueryService columnDefineQueryService;
	
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.columndefine.ColumnDefineBizService#addColumnDefine(ColumnDefineEntity columnDefineEntity)
	 * @param ColumnDefineEntity columnDefineEntity
	 * @return int
	 */
	public ResponseEntity addColumnDefine(ColumnDefineEntity columnDefineEntity) {
			ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
			  	 int i =  columnDefineDmlService.addColumnDefine(columnDefineEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.columndefine.ColumnDefineBizService#updateColumnDefine(ColumnDefineEntity columnDefineEntity)
	 * @param ColumnDefineEntity columnDefineEntity
	 * @return ResponseEntity
	 */
	public ResponseEntity updateColumnDefine(ColumnDefineEntity columnDefineEntity) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = columnDefineDmlService.updateColumnDefine(columnDefineEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.columndefine.ColumnDefineBizService#queryAllColumnDefine(ColumnDefineEntity columnDefineEntity)
	 * @param ColumnDefineEntity columnDefineEntity
	 * @return String
	 */
	public String queryAllColumnDefine(ColumnDefineEntity columnDefineEntity) {
		return JSON.toJSONString(columnDefineQueryService.queryAllColumnDefine(columnDefineEntity));
	} 
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.columndefine.ColumnDefineBizService#queryAllColumnDefine(ColumnDefineEntity columnDefineEntity)
	 * @param ColumnDefineEntity columnDefineEntity
	 * @return PageResult
	 */
	public String queryListForPageColumnDefine(ColumnDefineEntity columnDefineEntity) {
		return new PageResult( columnDefineQueryService.queryListForPageColumnDefine(columnDefineEntity), columnDefineQueryService.queryCountColumnDefine(columnDefineEntity)).toString();
	} 
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.columndefine.ColumnDefineQueryService#queryById(ColumnDefineEntity columnDefineEntity)
	 * @param BigInteger id
	 * @return
	 */
	public ColumnDefineEntity queryById(BigInteger id) {
		return columnDefineQueryService.queryById(id);
	}
	
	public void exportColumnDefine(ColumnDefineEntity columnDefineEntity, HttpServletResponse response) {
		List<ColumnDefineEntity> exportList = null;
		String ids = columnDefineEntity.getIds();
		if (ValidationUtil.isNullOrEmptyString(ids)) {
			exportList =columnDefineQueryService.queryAllColumnDefine(columnDefineEntity);
		} else {
			String[] idArray = ids.split(",");
			List<BigInteger> idList = new ArrayList<BigInteger>();
			for(String id : idArray) {
				idList.add(new BigInteger(id));
			}
			exportList = columnDefineQueryService.queryByIds(idList);
			// 将字典中的code转换为name
		   //convertCodeToName(exportList);
		}
		ExcelExport.exportExcel(exportList, "ColumnDefineEntityExcel", "ColumnDefineEntity信息", response);
	}
	
}

